import React from 'react'
import {connect} from 'react-redux';

import App from '../../common/App.jsx'
import Utils from '../../common/U.jsx'
import U from '../../common/U.jsx'
import HtmlEditor from '../common/HtmlEditor.jsx'
import OSSWrap from '../../common/OSSWrap';
import {
    Card,
    Form,
    Input,
    Upload,
    Tooltip,
    Icon,
    Radio,
    Progress,
    Button,
    message, InputNumber
} from 'antd';

import {Link} from 'react-router';
import BreadcrumbCustom from '../BreadcrumbCustom'
import CTYPE from "../../common/CTYPE";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;

const URL = {
    first: '管理员',
    second: '管理员编辑',
    save: 'adm/admin/save',
    detail: 'adm/admin/admin',
    go: 'app/admin/admins',
    key: 'adminId',
};

class AdminsEdit extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            progress: 0,
            namespace: '',
            id: parseInt(this.props.params.id),
        };
    }

    componentDidMount() {
        this.loadData();
    }

    loadData = () => {
        let _this = this;
        let id = _this.state.id;
        if (id && id > 0) {
            App.api(URL.detail, {[URL.key]: id}).then((data) => {
                _this.props.form.setFieldsValue({...data});
            })
        } else {
            console.log();
        }
    };


    handleSubmit = (e) => {
        let _this = this;
        e.preventDefault();
        _this.props.form.validateFields((err, value) => {
            if (err) {
                Object.keys(err).forEach(key => {
                    message.warning(err[key].errors[0].message);
                });
            }

            App.api(URL.save, {
                ...value,
            }).then(() => {
                message.success('添加成功');
                URL.go && App.go(URL.go);
            })

        });
    };


    render() {
        let _this = this;
        let id = this.props.params.id;
        const {getFieldDecorator} = _this.props.form;
        return (
            <div className="common-edit-page">

                <BreadcrumbCustom first={URL.first} second={URL.second}/>

                <Form onSubmit={this.handleSubmit}>
                    <Card title="编辑图文" bordered={false}
                          extra={<Button type="primary" htmlType="submit">{id > 0 ? '保存' : '发布'}</Button>}
                          style={{minWidth: CTYPE.eidtMinWidth, maxWidth: CTYPE.eidtMaxWidth}}>
                        <FormItem
                            {...CTYPE.formItemLayout}
                            label={(
                                <span>姓名</span>
                            )}
                            hasFeedback>
                            {getFieldDecorator('name', {})(
                                <Input/>
                            )}
                        </FormItem>

                        <FormItem
                            {...CTYPE.formItemLayout}
                            label={(
                                <span>邮箱</span>
                            )}
                            hasFeedback>
                            {getFieldDecorator('email', {})(
                                <Input/>
                            )}
                        </FormItem>

                        <FormItem
                            {...CTYPE.formItemLayout}
                            label={(
                                <span>密码</span>
                            )}
                            hasFeedback>
                            {getFieldDecorator('password', {})(
                                <Input/>
                            )}
                        </FormItem>

                    </Card>
                </Form>
            </div>
        )

    }
}

const CourseArticleEdit = Form.create()(AdminsEdit);

export default CourseArticleEdit;
